using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using B.S.RbacData.ErrorCode;
using SupplierCustomerManagement.Read.API.Applications.Commands.productpurchaseprices;

namespace SupplierCustomerManagement.Read.API.Controllers
{
    /// <summary>
    /// 商品采购价格查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ProductPurchasePricesController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMapper _mapper;
        private readonly ILogger<ProductPurchasePricesController> _logger;

        public ProductPurchasePricesController(
            IMediator mediator,
            IMapper mapper,
            ILogger<ProductPurchasePricesController> logger)
        {
            _mediator = mediator;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 获取商品采购价格列表
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpGet]
        public async Task<ApiPaging<Dtos.ProductPurchasePriceListDto>> GetProductPurchasePrices([FromQuery] GetProductPurchasePricesQuery query)
        {
            try
            {
                // 发送查询命令
                var result = await _mediator.Send(query);

                _logger.LogInformation("成功获取商品采购价格列表，总数: {TotalCount}, 页码: {PageIndex}, 页大小: {PageSize}", 
                    result.TotalCount, query.PageIndex, query.PageSize);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取商品采购价格列表时发生异常");
                throw;
            }
        }

    }
} 